package com.easyskool.util;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public class mailUtil implements Runnable {
	String reciever;
	String subject;
	String content;
	 Thread t;
	 public mailUtil(String reciever,String subject,String content) {
	      // Create a new, second thread
			this.reciever=reciever;
			this.subject=subject;
			this.content=content;
	      t = new Thread(this, "Demo Thread");
	      System.out.println("Child thread: " + t);
	      t.start(); // Start the thread
	   }
	   
	   // This is the entry point for the second thread.
	   public void run() {
	      final String username = "easyskool.com@gmail.com";
		final String password = "18111991";
     System.out.println("in mailllllllllllllllllllllllll");
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
    
		Session session = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  });
    
		try {
    
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("easyskool.com@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse( reciever));
			message.setSubject(subject);
//			message.setText(content);
			message.setContent(content, "text/html; charset=utf-8");
    
			Transport.send(message);
    
			System.out.println("Done "+this.reciever);
    
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	     System.out.println("Exiting child thread.");
	   }

	public  void sendMail(String reciever,String subject,String content) {
		this.reciever=reciever;
		this.subject=subject;
		this.content=content;
		 
	
	}
	public static void main(String q[])
	{
//		new mailUtil().sendMail("singh.ishupreet@gmail.com");
	}
}